import { getErrorQuestions, saveErrorCourse, searchTreeByTime } from '../../../service/api';
import { myMessage } from '../../../utils/formatDataSource';

export default {
  namespace: 'wrongs1',

  state: {
    questionInfo: {},
    treeList: [],
  },

  effects: {
    *questions({ payload }, { call, put }) {
      const response = yield call(getErrorQuestions, payload);
      yield put({
        type: 'saveQuestions',
        payload: response,
      });
    },
    *saveError({ payload }, { call, put }) {
      const response = yield call(saveErrorCourse, payload);
      yield put({
        type: 's_saveError',
        payload: response,
      });
    },
    *searchTree({ payload }, { call, put }) {
      const response = yield call(searchTreeByTime, payload);
      yield put({
        type: 's_searchTree',
        payload: response,
      });
    },
  },

  reducers: {
    saveQuestions(state, action) {
      if (action.payload) {
        state.questionInfo = action.payload.data;
      }
      return {
        ...state,
      };
    },
    s_saveError(state, action) {
      const status = action.payload.status;
      if (status === 1) {
        myMessage.success('保存成功', 2);
      } else {
        myMessage.error('保存失败', 2);
      }
      return {
        ...state,
      };
    },
    s_searchTree(state, action) {
      if (action.payload) {
        state.treeList = action.payload.data;
      }
      return {
        ...state,
      };
    },
  },
};
